package cn.pet_know.service.impl;

import cn.pet_know.common.constants.DelFlag;
import cn.pet_know.common.my_exception.CustomException;
import cn.pet_know.common.utils.ConverterUtils;
import cn.pet_know.common.utils.GenIdUtil;
import cn.pet_know.dao.mapper.KnowledgeMapper;
import cn.pet_know.entity.KnowledgeTypeWith;
import cn.pet_know.entity.dto.KnowledgeDTO;
import cn.pet_know.entity.vo.KnowledgeVO;
import cn.pet_know.service.KnowledgeService;
import cn.pet_know.entity.Knowledge;
import cn.pet_know.service.KnowledgeTypeWithService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class KnowledgeServiceImpl extends ServiceImpl<KnowledgeMapper, Knowledge> implements KnowledgeService {

    @Resource
    private KnowledgeTypeWithService knowledgeTypeWithService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(KnowledgeDTO knowledgeDTO) {
        // 解析文本类型
        String knowledgeTypes = knowledgeDTO.getKnowledgeTypes();
        String[] split = knowledgeTypes.split(",");
        ArrayList<KnowledgeTypeWith> knowledgeTypeWiths = new ArrayList<>();
        // 存储关联关系
        for (String typeId : split) {
            KnowledgeTypeWith knowledgeTypeWith = new KnowledgeTypeWith();
            knowledgeTypeWith.setTypeId(Long.valueOf(typeId));
            knowledgeTypeWith.setKnowledgeId(knowledgeDTO.getKnowledgeId());
            knowledgeTypeWiths.add(knowledgeTypeWith);
        }
        knowledgeTypeWithService.saveBatch(knowledgeTypeWiths);

        Knowledge knowledge = ConverterUtils.copyR(knowledgeDTO, new Knowledge());
        long uid = GenIdUtil.generatorId();
        knowledge.setKnowledgeId(uid);
        return this.save(knowledge);
    }

    @Override
    public List<KnowledgeVO> listKnowledge(KnowledgeDTO knowledgeDTO) {
        List<Knowledge> list = this.list(new LambdaQueryWrapper<Knowledge>().setEntity(ConverterUtils.copyR(knowledgeDTO, new Knowledge())).eq(Knowledge::getDeleted,DelFlag.EXISTS));
        return ConverterUtils.listCopyR(list, KnowledgeVO.class);
    }

    @Override
    public Boolean del(KnowledgeDTO knowledgeDTO) {
        if (Objects.isNull(knowledgeDTO.getKnowledgeId())) {
            throw new CustomException("knowLedgeId is null !");
        }

        return this.update(
                new LambdaUpdateWrapper<Knowledge>()
                        .set(Knowledge::getDeleted, DelFlag.DELETED)
                        .eq(Knowledge::getKnowledgeId, knowledgeDTO.getKnowledgeId())
        );
    }

    @Override
    public Page<KnowledgeVO> queryPage(KnowledgeDTO knowledgeDTO) {
        Page<Knowledge> knowledgePage = new Page<>();
        knowledgePage.setSize(knowledgeDTO.getPageSize());
        knowledgePage.setCurrent(knowledgeDTO.getPageNo());
        Page<Knowledge> page = this.page(knowledgePage, new LambdaQueryWrapper<Knowledge>().setEntity(ConverterUtils.copyR(knowledgeDTO, new Knowledge())).eq(Knowledge::getDeleted,DelFlag.EXISTS));
        return ConverterUtils.pageCopyR(page, KnowledgeVO.class);
    }

    @Override
    public Boolean review(KnowledgeDTO knowledgeDTO) {
        if (Objects.isNull(knowledgeDTO.getKnowledgeId())) {
            throw new CustomException("knowLedgeId is null !");
        }
        if (Objects.isNull(knowledgeDTO.getKnowledgeStatus())) {
            throw new CustomException("knowLedgeStatus is null !");
        }
        return this.update(
                new LambdaUpdateWrapper<Knowledge>()
                        .set(Knowledge::getKnowledgeStatus,knowledgeDTO.getKnowledgeStatus())
                        .eq(Knowledge::getKnowledgeId,1)
        );
    }

    @Override
    public Boolean knowUpdate(KnowledgeDTO knowledgeDTO) {
        if (Objects.isNull(knowledgeDTO.getKnowledgeText()) || knowledgeDTO.getKnowledgeText().length() > 10000){
            throw new CustomException("knowledgeText is null or length > 10000");
        }
        return this.update(
                new LambdaUpdateWrapper<Knowledge>()
                        .eq(Knowledge::getKnowledgeId,knowledgeDTO.getKnowledgeId())
                        .set(Objects.nonNull(knowledgeDTO.getKnowledgeTitle()),Knowledge::getKnowledgeTitle,knowledgeDTO.getKnowledgeTitle())
                        .set(Objects.nonNull(knowledgeDTO.getKnowledgeText()),Knowledge::getKnowledgeText,knowledgeDTO.getKnowledgeText())
        );
    }
}